#include "head.h"

int repeatCheck(pcustomer phead, int id)
{
	pcustomer pcur = phead;
	while (pcur)
	{
		if (id == pcur->id)
		{
			return 1;
		}
		pcur = pcur->pnext;
	}
	return -1;
}
int tailInsert(pcustomer* pphead, pcustomer* pptail, int id, char* name, int room_num)
{
	int ret=-1;//查找的返回值，用于判断是否有客户信息
	ret = repeatCheck(*pphead, id);
	if (1==ret)
	{
		printf("客户id不可以重复！\n");
		return -1;
	}
	pcustomer pcur;
	pcur = (pcustomer)malloc(sizeof(customer));
	memset(pcur, 0, sizeof(customer));
	//pcur = (pcustomer)calloc(1,sizeof(customer));
	pcur->id = id;
	strcpy(pcur->name, name);//pcur->name为一个地址
	pcur->room_num = room_num;
	if (!*pphead)
	{
		*pphead = *pptail = pcur;
	}
	else
	{
		(*pptail)->pnext = pcur;//尾插法实现链表的插入
		*pptail = pcur;
	}
	return 1;
}
void printList(pcustomer phead)
{
	if (NULL == phead)
	{
		printf("没有客户信息！\n");
		return;
	}
	pcustomer pcur = phead;
	while (pcur)
	{
		printf("客户id：%d 客户姓名：%s 客户所住客房编号：%d\n", pcur->id, pcur->name, pcur->room_num);
		pcur = pcur->pnext;
	}
}
int findId(pcustomer phead, int id)
{
	int flag = 0;
	if (NULL == phead)
	{
		printf("没有客户信息！\n");
		return 0;
	}
	pcustomer pcur = phead;
	while (pcur)
	{
		if (id == pcur->id)
		{
			flag = 1;
			printf("客户id：%d 客户姓名：%s 客户所住客房编号：%d\n", pcur->id, pcur->name, pcur->room_num);
		}
		pcur = pcur->pnext;
	}
	if (0 == flag)
	{
		printf("没有对应客户信息！\n");
		return 0;
	}
	return 1;
}
void modifyList(pcustomer phead, int id, int room_num)
{
	pcustomer pcur = phead;
	while (pcur)
	{
		if (id == pcur->id)
		{
			pcur->room_num = room_num;
		}
		pcur = pcur->pnext;
	}
}
void deleteList(pcustomer* pphead, pcustomer* pptail, int id)
{
	pcustomer pcur = *pphead;
	pcustomer ppre = *pphead;
	if (*pphead == *pptail)
	{
		free(pcur);
		*pphead = *pptail = NULL;
	}
	else
	{
		if (id == (*pphead)->id)//删除对象为头指针
		{
			(*pphead) = (*pphead)->pnext;
			free(pcur);
		}
		else if (id == (*pptail)->id)//删除对象为尾指针
		{
			ppre = *pphead;
			pcur = (*pphead)->pnext;
			while ((*pptail) != pcur)
			{
				ppre = ppre->pnext;
				pcur = pcur->pnext;
			}
			(*pptail) = ppre;
			ppre->pnext = NULL;
			free(pcur);
		}
		else//其他情况
		{
			pcur = pcur->pnext;
			while (id != pcur->id)
			{
				ppre = ppre->pnext;
				pcur = pcur->pnext;
			}
			ppre->pnext = pcur->pnext;
			free(pcur);
		}
	}
}
int mycompare(const void* left, const void* right)
{
	elem_t* p1 = (elem_t*)left;
	elem_t* p2 = (elem_t*)right;
	if ((*p1)->id - (*p2)->id > 0)
	{
		return 1;
	}
	else if ((*p1)->id - (*p2)->id < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}
int comparePointer(const void* left, const void* right)
{
	elem_t *p1 = (elem_t*)left;
	elem_t *p2 = (elem_t*)right;
	return (*p1)->id - (*p2)->id;
}
void printsorted(pcustomer* parr,int len)
{
	int i;
	pcustomer pcur;
	for (i = 0; i < len; ++i)
	{
		pcur = parr[i];
		printf("客户id：%d 客户姓名：%s 客户所住客房编号：%d\n", pcur->id, pcur->name, pcur->room_num);
	}
}